<!--
 * @Author: Rain
 * @Date: 2021-11-22 11:47:50
 * @LastEditTime: 2021-12-08 22:19:40
 * @Description: Do Something
-->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div class="w-full flex items-center flex-end search">
        <!-- 搜索 -->
        <el-select v-model="query.type" filterable placeholder="请选择">
          <el-option
            v-for="item in dict.report_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
        <crudOperation :permission="permission" />
      </div>

      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="报告类型">
            <el-select v-model="form.type" filterable placeholder="请选择" class="w-full">
              <el-option
                v-for="item in dict.report_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="测试类型">
            <el-select
              v-model="form.atmrId"
              filterable
              placeholder="请选择"
              class="w-full"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item | optionFitler(dict.test_type)"
                :value="item.id + ''"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="得分">
            <div class="w-full flex items-center">
              <el-input v-model="form.totalMin" style="width: 170px;" />
              -
              <el-input v-model="form.totalMax" style="width: 170px;" />
            </div>
          </el-form-item>
          <el-form-item label="报告路径">
            <el-input v-model="form.reportUrl" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="type" label="报告类型">
          <template slot-scope="scope">
            {{ dict.label.report_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="totalMin" label="得分" align="center">
          <template slot-scope="{row}">
            <div>
              {{ row.totalMin }}-{{ row.totalMax }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="reportUrl" label="报告路径" />
        <el-table-column prop="atmrId" label="测试类型">
          <template slot-scope="{row}">
            <div>
              {{ row.atmrId | examFitler(typeList,dict.test_type) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-if="checkPer(['admin','examReport:edit','examReport:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudExamReport from '@/api/report'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'

const defaultForm = { id: null, type: null, totalMin: null, totalMax: null, reportUrl: null, remark: null, atmrId: null }
export default {
  name: 'Report',
  components: { pagination, crudOperation, rrOperation, udOperation },
  filters: {
    examFitler(value, typeList, list) {
      const item = typeList.find(item => item.id * 1 === value * 1)
      if (item) {
        const { atmrType, atmrName } = item
        const type = list.find(i => i.value === atmrType)
        return `${atmrName}-${type.label}`
      }
    },
    optionFitler(item, list) {
      const { atmrName, atmrType } = item
      const type = list.find(i => i.value === atmrType)
      return `${atmrName}-${type.label}`
    }
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['report_type', 'test_type'],
  cruds() {
    return CRUD({ title: '测试报告', url: 'api/examReport', idField: 'id', sort: 'id,desc', crudMethod: { ...crudExamReport }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'examReport:add'],
        edit: ['admin', 'examReport:edit'],
        del: ['admin', 'examReport:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'type', display_name: '报告类型' }
      ],
      typeList: []
    }
  },
  mounted() {
    this.queryTestType()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 查询测试类型
    queryTestType() {
      this.$http.get(`/api/atmr`).then(_ => {
        const { content } = _
        this.typeList = content
      })
    }
  }
}
</script>

<style scoped>

</style>
